﻿using Microsoft.Extensions.DependencyInjection;
using Qianke.Core.Common.Interface;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Users;

namespace Qianke.Core.Saas.WebApi.SaasExtensions.SaasAuditing
{

    /// <summary>
    /// 操作员 审计日志Contributor
    /// </summary>
    public class SaasTokenInfoAuditLogContributor:AuditLogContributor, ITransientDependency
    {

        public override void PostContribute(AuditLogContributionContext context)
        {
            //日志部分是新开的作用域 所以拿不到TokenInfo 对象了
            //var tokenInfo = context.ServiceProvider.GetService<ITokenInfo>();
            var currentUser = context.ServiceProvider.GetService<ICurrentUser>();

            Trace.WriteLine(currentUser?.FindClaimValue(ClaimTypes.NameIdentifier));
            context.AuditInfo.ExtraProperties["Id"] = currentUser?.FindClaimValue(ClaimTypes.NameIdentifier);
            context.AuditInfo.ExtraProperties["OrganId"] = currentUser?.FindClaimValue("OrganId");
            context.AuditInfo.ExtraProperties["Name"] = currentUser?.FindClaimValue(ClaimTypes.Name);
        }
    }
}
